var contract = function(){
		this.btype =2; //默认的是个人的项目
}
//添加约定放款
contract.prototype.addfk = function(obj,names){
    var num = $(obj).find('tr').size();
    var id = names+'_'+num;
    if(names == 'plan_action_time_2'){
    	var text = '<tr id="'+id+'"><td><span class=\'delone\'>删除</span></td><td><input type=\'text\' id=""  class=\'easyui-datebox\' name=\''+names+'[]\' style=\'width:150px;height:30px;\'/></td><td><input type=\'text\' id=\'interest_rates\' data-options="delay:1000,height:30,validType:\'eglt[0,1000000000]\',precision:2" class=\'easyui-numberbox\'  name=\''+names+'_money[]\'/></td><td><input type=\'text\' id=\'interest_rates\' class=\'easyui-numberbox\' data-options="delay:1000,height:30,validType:\'eglt[0,1000000000]\',precision:2" name=\'interest_account[]\'/></td></tr>';
    }else{
    	var text = '<tr id="'+id+'"><td><span class=\'delone\'>删除</span></td><td><input type=\'text\' id=""  class=\'easyui-datebox\' name=\''+names+'[]\' style=\'width:150px;height:30px;\'/></td><td><input type=\'text\' id=\'interest_rates\' class=\'easyui-numberbox\' data-options="delay:1000,height:30,validType:\'eglt[0,1000000000]\',precision:2" name=\''+names+'_money[]\'/></td></tr>';
    }
	
	obj.append(text);
	$.parser.parse($('#'+id));
	$(".delone").bind('click',function(){
		$(this).parent().parent().remove();
	});
}
 

//修改约定的放款的实现 

contract.prototype.addUpdate = function(obj,names,data){
    var num = $(obj).find('tr').size();
    var id = names+'_'+num;
    if(names == 'plan_action_time_2'){
    	var text = '<tr id="'+id+'"><td><input type="hidden" name="'+names+'_input[]" value="'+obj.id+'"/><span class=\'delone\'>删除</span></td><td><input type=\'text\' id="" value='+data.plan_action_time+'  class=\'easyui-datebox\' name=\''+names+'[]\' style=\'width:150px;height:30px;\'/></td>';
    		text +='<td><input type=\'text\' id=\'interest_rates\' class=\'easyui-numberbox\' data-options="delay:1000,height:30,validType:\'eglt[0,10000000]\',precision:2" value='+data.action_account+'  name=\''+names+'_money[]\'/></td>';
    		text +='<td><input type=\'text\' id=\'interest_rates\' class=\'easyui-numberbox\' data-options="delay:1000,height:30,validType:\'eglt[0,10000000]\',precision:2" name=\'interest_account[]\' value ='+data.interest_account+'/></td></tr>';
    }else{
    	var text = '<tr id="'+id+'"><td><input type="hidden" name="'+names+'_input[]" value="'+obj.id+'"/><span class=\'delone\'>删除</span></td><td><input type=\'text\' id="" value='+data.plan_action_time+'  class=\'easyui-datebox\' name=\''+names+'[]\' style=\'width:150px;height:30px;\'/></td><td><input type=\'text\' id=\'interest_rates\' data-options="delay:1000,height:30,validType:\'eglt[0,1000000000]\',precision:2" class=\'easyui-numberbox\' value='+data.action_account+'  name=\''+names+'_money[]\'/></td></tr>';
    }
	
	obj.append(text);
	$.parser.parse($('#'+id));
	$(".delone").bind('click',function(){
		$(this).parent().parent().remove();
	});
}

function checkStatus(value){
	switch(value){
		case '1' :
			return '<font color="green">男</font>';
		case '2' :
			return '<font color="green">女</font>';
	}

}

//创建权益人个人选项
contract.prototype.selectSingleContractList = function(getSearchBorrowerListUrl){
	$("#borrower_type").val(2);
	$('#searchSingleBorrowerList').datagrid({
		url:getSearchBorrowerListUrl,
		fitColumns:true,
		 queryParams: {
		        'is_company':0},
		rownumbers:true,
		pageSize:10,
		pageNumber:1,
    	singleSelect:true,
		pageList:[2,5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'name',title:'姓名'},
		{field:'identity_num',title:'身份证号'},
		{field:'mobile',title:'手机号码'},
		//{field:'yn_company',title:'公司',formatter:checkYesNo},
		{field:'company_name',title:'固定电话'},
	
		{field:'sex',title:'性别',formatter:checkStatus},
		
		{field:'s_id',title:'公章',formatter:checkGongzhuang}
		]],
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#searcheBorrowerDilog').tabs('getSelected');
				var index=$('#searcheBorrowerDilog').tabs('getTabIndex',tab);
				$('#searcheBorrowerDilog').tabs('close',index);
			});
		},
		pagination:true
	});
	$('#searcheBorrowerDilog').dialog({
		title:'选择借款人',
		modal:true,
		width:600,
		height:400,
		buttons:'#searBorrowerButton'
	});
	
};

//创建权益人企业选项 根据传入的参数 标识是个人还是企业
contract.prototype.selectCompanyContractList = function(getSearchBorrowerListUrl){
	this.btype = 1; //表示现在提交的是企业的信息
	$("#borrower_type").val(1);
	$('#searchSingleBorrowerList').datagrid({
		url:getSearchBorrowerListUrl,
		fitColumns:true,
		rownumbers:true, 
		queryParams: {
	        'is_company':1},
		pageSize:10,
		pageNumber:1,
    	singleSelect:true,
		pageList:[2,5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'all_name',title:'公司名称',sortable:true},
		{field:'corporate',title:'法人'},
		{field:'licences_code',title:'营业执照编号'},
		{field:'organize_code',title:'组织机构代码'},
		{field:'tax_code',title:'税务登记号'},
		{field:'s_id',title:'公章',formatter:checkGongzhuang}
		]],
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#searcheBorrowerDilog').tabs('getSelected');
				var index=$('#searcheBorrowerDilog').tabs('getTabIndex',tab);
				$('#searcheBorrowerDilog').tabs('close',index);
			});
		},
		pagination:true
	});
	$('#searcheBorrowerDilog').dialog({
		title:'选择借款企业',
		modal:true,
		width:600,
		height:400,
		buttons:'#searBorrowerButton'
	});
};


//选择相关的
contract.prototype.submitfunc = function(id){
	
	var authRuleRow =$('#searchSingleBorrowerList').datagrid('getSelections');
	if (authRuleRow.length<1){		            	
		$.messager.alert('提示','请选择借款人!','info');
	}else if(authRuleRow.length > 1){
		$.messager.alert('提示','只能选择一个借款人!','info');
	}else{
		if(authRuleRow[0].s_id){
			$('#'+id).form('load',{
				borrower_id:authRuleRow[0].borrower_id,
				borrower_name:authRuleRow[0].all_name,
			});		
			$('#searcheBorrowerDilog').dialog('close');
		}else{
			$.messager.alert('警告','您选择的借款人还没有印章，找法务制章后再签约！');
		}
	}

};

//放款的总额不能大于合同的总额
function check_input(name,number){
	var users = document.getElementsByName(name);
	var val_array = 0;
	for(var i=0; i<users.length; i++){
		  val_array += parseFloat(users[i].value);
    }
    console.log(val_array);
	return (isNaN(val_array) || val_array <= parseFloat(number))? true : false;

}

//添加内容
//添加模板

var ocontract = new contract();

$(function(){
	$(".delone").bind('click',function(){
		$(this).parent().parent().remove();
	});
	


})


function verifyCheck(value){
	switch(value){
        case '1' : 
            return  '<font color="#A3A3A3"><a>待提交</a></font>';
        case '-1' : 
            return  '<font color="#CD2626">驳回</font>';
        case '2' : 
            return  '<font color="#CD853F">待审核</font>';
        case '3' : 
            return  '<font color="#1C86EE">审核通过</font>';
        case '4' : 
            return  '<font color="#333">合同结束</font>';
	}
}

//计算天数差的函数，通用  
function  dateDiff(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式  
    var  aDate,  oDate1,  oDate2,  iDays  
    aDate  =  sDate1.split("-")  
    oDate1 = Date.UTC(aDate[0],aDate[1]-1,aDate[2]);
    aDate  =  sDate2.split("-")  
    oDate2 = Date.UTC(aDate[0],aDate[1]-1,aDate[2]);
    iDays  =  parseInt((oDate2 - oDate1)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
    return  iDays -1 +2;  
}
//统计某列数据
function compute(obj,colName,filterV) {
    var rows = obj.datagrid('getRows');
    var total = 0;
    for (var i = 0; i < rows.length; i++) {
        if(!filterV || rows[i]['status'] == filterV)
            total += parseFloat(rows[i][colName]);
    }
    return isNaN(total) ? 0 : total;
}

function comRows(obj,colName) {
    var rows = obj.datagrid('getRows');
    var comArr = new Array();
    
    for (var i = 0; i < rows.length; i++) {
        if(!inArray(comArr,rows[i][colName]))
            comArr.push(rows[i][colName]);
    }
    console.log(comArr.length);
    return comArr.length;
}

function inArray(arr, val){ 
    for(var i=0; i<arr.length; i++){ 
        if(arr[i] == val) 
            return true; 
    } 
    return false; 
}
